package openai

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

type AnalysisIndustry struct {
	Client  *azopenai.Client
	Message *[]azopenai.ChatRequestMessageClassification
	Resp    *response
}

func (a *AnalysisIndustry) SetMessage(ctx context.Context) {
	// This is a conversation in progress.
	// NOTE: all messages, regardless of role, count against token usage for this API.
	messages := []azopenai.ChatRequestMessageClassification{
		// You set the tone and rules of the conversation with a prompt as the system role.
		&azopenai.ChatRequestSystemMessage{Content: to.Ptr("You are a helpful assistant. You will talk like a 经济学家.")},

		// The user asks a question
		&azopenai.ChatRequestUserMessage{Content: azopenai.NewChatRequestUserMessageContent("Can you help me?")},

		// The reply would come back from the ChatGPT. You'd add it to the conversation so we can maintain context.
		&azopenai.ChatRequestAssistantMessage{Content: to.Ptr("Arrrr! Of course, me hearty! What can I do for ye?")},

		// The user answers the question based on the latest reply.
		&azopenai.ChatRequestUserMessage{Content: azopenai.NewChatRequestUserMessageContent("用中文回答我的问题，鲁迅为什么要打周树人")},

		// from here you'd keep iterating, sending responses back from ChatGPT
	}
	a.Message = &messages
}

func (c *AnalysisIndustry) GetResp(ctx context.Context, modelDeploymentID string) {
	{
		gotReply := false

		resp, err := c.Client.GetChatCompletions(context.TODO(), azopenai.ChatCompletionsOptions{
			// This is a conversation in progress.
			// NOTE: all messages count against token usage for this API.
			Messages:       *c.Message,
			DeploymentName: &modelDeploymentID,
		}, nil)

		if err != nil {
			//  TODO: Update the following line with your application specific error handling logic
			log.Fatalf("ERROR: %s", err)
		}

		for _, choice := range resp.Choices {
			gotReply = true

			if choice.ContentFilterResults != nil {
				fmt.Fprintf(os.Stderr, "Content filter results\n")

				if choice.ContentFilterResults.Error != nil {
					fmt.Fprintf(os.Stderr, "  Error:%v\n", choice.ContentFilterResults.Error)
				}

				fmt.Fprintf(os.Stderr, "  Hate: sev: %v, filtered: %v\n", *choice.ContentFilterResults.Hate.Severity, *choice.ContentFilterResults.Hate.Filtered)
				fmt.Fprintf(os.Stderr, "  SelfHarm: sev: %v, filtered: %v\n", *choice.ContentFilterResults.SelfHarm.Severity, *choice.ContentFilterResults.SelfHarm.Filtered)
				fmt.Fprintf(os.Stderr, "  Sexual: sev: %v, filtered: %v\n", *choice.ContentFilterResults.Sexual.Severity, *choice.ContentFilterResults.Sexual.Filtered)
				fmt.Fprintf(os.Stderr, "  Violence: sev: %v, filtered: %v\n", *choice.ContentFilterResults.Violence.Severity, *choice.ContentFilterResults.Violence.Filtered)
			}

			if choice.Message != nil && choice.Message.Content != nil {
				fmt.Fprintf(os.Stderr, "Content[%d]: %s\n", *choice.Index, *choice.Message.Content)
			}

			if choice.FinishReason != nil {
				// this choice's conversation is complete.
				fmt.Fprintf(os.Stderr, "Finish reason[%d]: %s\n", *choice.Index, *choice.FinishReason)
			}
		}

		if gotReply {
			fmt.Fprintf(os.Stderr, "Got chat completions reply\n")
		}
	}

}
